import moment from 'moment';
import { Storage } from "@/utils/Storage";
import { useRoute, useRouter } from "vue-router";
export function randomWord(randomFlag: Boolean, min: number, max: number) {
  // randomFlag: Boolean 是否随机个数
  // min 最少个数
  // max 最大个数
  var str = ''
  var range = min
  var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  // 随机产生
  if (randomFlag) {
    range = Math.round(Math.random() * (max - min)) + min
  }
  var pos = 0
  for (var i = 0; i < range; i++) {
    pos = Math.round(Math.random() * (arr.length - 1))
    str += arr[pos]
  }
  return str
}
// 获取日期
export function getData(dayNum: number,) {
  var myDate: number = +new Date();
  var lw = new Date(myDate - 1000 * 60 * 60 * 24 * dayNum)// 最后一个数字多少天前的意思
  var lastY = lw.getFullYear()
  var lastM = lw.getMonth() + 1
  var lastD = lw.getDate()
  var startdate = lastY + '-' + (lastM < 10 ? '0' + lastM : lastM) + '-' + (lastD < 10 ? '0' + lastD : lastD)
  return startdate
}
// 秒转换时分秒
export function transverterMss(time: number) {
  let result = (time || 0) / 1000;
  var h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600)
  var m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60))
  var s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60))
  return h + ':' + m + ':' + s
}
// 国际化时间格式转成本地时间格式
export function newDate(time: any) {
  // 假设全球时间是以UTC格式的字符串传递过来
  const globalTime = time;// 创建Date对象，并将全球时间转换为本地时间
  const date = new Date(globalTime);// 获取当地时间的年、月、日等信息
  const year = date.getFullYear();
  const month = date.getMonth() + 1; // 月份从0开始，需要加1
  const day = date.getDate();// 获取当地时间的小时、分钟、秒等信息
  const hours = date.getHours();
  const minutes = date.getMinutes();
  const seconds = date.getSeconds();// 将当地时间转换为当地格式的字符串
  const localTimeString = date.toLocaleString(); // 根据本地设置，转换为当地格式的字符串

  console.log("本地时间字符串:", localTimeString);
}

// utf-8解码

export function utf8ToCh(strUtf8: string) {
  var i, len, ch;
  let arr = [];
  len = strUtf8.length;
  for (i = 0; i < len; i++) {
    ch = strUtf8.charCodeAt(i);
    if ((ch & 0xff80) == 0) {
      arr.push(strUtf8.charAt(i));
    } else if ((ch & 0xfc00) == 0xd800) {
      i++;
      var rs: any = (ch & 0x3ff) << 10;
      ch = strUtf8.charCodeAt(i);
      rs |= ch & 0x3ff;
      rs += 0x10000;
      arr.push(String.fromCharCode(rs >> 18, rs >> 12 & 0x3f, rs >> 6 & 0x3f, rs & 0x3f));
    } else {
      arr.push(String.fromCharCode(ch >> 12 & 0x0f, ch >> 6 & 0x3f, ch & 0x3f));
    }
  }
  return arr.join('');
}

// 设置系统页签icon
// 系统设置
export function SET_SETTING_System_Icon(url: any) {
  if (url) {
    const href = url;
    var link: any = document.querySelector("link[rel*='icon']") || document.createElement('link')
    link.type = 'image/x-icon'
    link.rel = 'shortcut icon'
    link.href = href
    document.getElementsByTagName('head')[0].appendChild(link)
  }
}
export function SET_SETTING_System_Title(text: any) {
  if (text) {
    var title: any = document.querySelector('title');
    title.innerText = text;
  }
}
// 当前之后的日期不可选日期
export function disabledDate_after(current: any) {
  // 当前时间之前不可选(不包含当前日期)
  return current && current >= moment().subtract(0, 'days').endOf('day');
}

const range = (start: any, end: any) => {
  const result = [];
  for (let i = start; i < end; i++) {
    result.push(i);
  }
  return result;
}

export function disabledDateTime_after(current: any) {

  if (current) {
    console.log(moment().date(), current.date(), 'moment().date(), current.date()')
    let today = moment().date();
    if (today == current.date()) {
      let minute = Number(moment().minutes())
      let hour = Number(moment().hour());
      let finalHour: number, finalMinute: number;
      if (current.hour() > hour) {
        finalMinute = 0
      } else {
        if (current.minute() >= 58) {
          finalHour = hour + 1;
          finalMinute = 0;
        } else {
          finalHour = hour;
          finalMinute = minute;
        }
      }
      return {
        disabledHours: () => range(finalHour + 1, 24),
        disabledMinutes: () => range(finalMinute + 1, 60),
        disabledSeconds: () => range(0, 60)
      }
    } else if (moment().date() < current.date()) {
      return {
        disabledHours: () => range(0, 24),
        disabledMinutes: () => range(0, 60),
        disabledSeconds: () => range(0, 60)
      }
    }
  } else {
    return {
      disabledHours: () => range(0, 24),
      disabledMinutes: () => range(0, 60),
      disabledSeconds: () => range(0, 60),
    }
  }
};
// 当前日期之前的不可选
export function disabledDate_before(current: any) {
  return current && current <= moment().subtract(1, 'days').endOf('day'); // 选择时间要大于等于当前天。若今天不能被选择，去掉等号即可。
}
const range_before = (start: any, end: any) => {
  const result = [];
  for (let i = start; i <= end; i++) {
    result.push(i);
  }
  return result;
};
export function disabledDateTime_before(dates: any,) {
  let hours = moment().hours();//0~23
  let minutes = moment().minutes();//0~59
  let seconds = moment().seconds();//0~59
  //当日只能选择当前时间之后的时间点
  if (dates) {
    if (moment().date() == dates.date()) {
      return {
        disabledHours: () => range_before(0, hours - 1),
        disabledMinutes: () => range_before(0, minutes - 1),
        disabledSeconds: () => range_before(0, seconds - 1),
      };
    } else if (moment().date() > dates.date()) {
      return {
        disabledHours: () => range(0, 24),
        disabledMinutes: () => range(0, 60),
        disabledSeconds: () => range(0, 60)
      }
    }
  } else {
    return {
      disabledHours: () => range(0, 24),
      disabledMinutes: () => range(0, 60),
      disabledSeconds: () => range(0, 60)
    }
  }

};

// 按钮权限方法
export function treeToTile(treeData: any[], childKey = "children") {
  const arr: any[] = [];
  const expanded = (data: any[], com: any) => {
    if (data && data.length > 0) {
      data
        .filter((d) => d)
        .forEach((e) => {
          arr.push(e);
          expanded(e[childKey] || [], e.component);
        });
    }
  };
  expanded(treeData, '');
  return arr;
};
export function routerPerm() {
  const path = useRoute().path;
  let floatData: any[] = []
  if (sessionStorage.getItem('user')) {
    floatData = treeToTile(JSON.parse(sessionStorage.getItem('user') || '')?.userInfo?.menues || []);
  }
  let arr: any = [];
  floatData.forEach((item: any) => {
    if (item.routePath == path) {
      if (item.children && item.children.length) {
        item.children.forEach((cData: any) => {
          if (cData.menuType != 1) {
            arr.push(cData.permIdentity)
          }
        })
      }
    } else if (item.entryPage == path) {
      arr.push(item.permIdentity)
    }
  });
  return arr;
}


